﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using NopSolutions.NopCommerce.BusinessLogic.Products;
using NopSolutions.NopCommerce.Common.Utils;

namespace NopSolutions.NopCommerce.Web.Modules
{
  public partial class ProductFiles : BaseNopFrontendUserControl
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!Page.IsPostBack)
      {
        BindData();
      }

    }

    protected void BindData()
    {
      var product = this.ProductService.GetProductById(this.ProductId);
      if (product != null)
      {
        var productFiles = product.ProductFiles;
        if (productFiles.Count > 0)
        {
          rptrProductFiles.DataSource = productFiles;
          rptrProductFiles.DataBind();
        }
      }
      else
        this.Visible = false;
    }

    protected void rptrProductFiles_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
      if ((e.Item.ItemType == ListItemType.AlternatingItem) || (e.Item.ItemType == ListItemType.Item))
      {
        ProductFile productFile = (ProductFile)e.Item.DataItem;
        HyperLink hlFileName = e.Item.FindControl("hlFileName") as HyperLink;
        if (hlFileName != null)
        {
          hlFileName.Text = productFile.NpFile.Filename + productFile.NpFile.Extension;
          hlFileName.NavigateUrl = this.DownloadService.GetPublicFileDownloadUrl(productFile.NpFile);
        }
      }
    }

    public int ProductId
    {
      get
      {
        return CommonHelper.QueryStringInt("ProductId");
      }
    }
  }
}